package com.jdbc;

import java.sql.*;

/**
 *
 * Created by CCX on 2018/6/27.
 */
public class JDBCConnectionUtil {
    //驱动程序名
    private final String   driver = "com.mysql.jdbc.Driver";
    //MySQL配置时的密码
    private final String user = "root";

    private final String password = "123456";
    private static JDBCConnectionUtil jdbcConnectionUtil;
    private Connection connection;
    private Statement statement = null;
    private  ResultSet rs;

    private JDBCConnectionUtil(){

    }

    public static JDBCConnectionUtil getInstate(){
        if(jdbcConnectionUtil==null){
            jdbcConnectionUtil =new JDBCConnectionUtil();
        }
        return jdbcConnectionUtil;
    }


    public void connectionMySql(){
        //声明Connection对象

        //URL指向要访问的数据库名mydata
        String url = "jdbc:mysql://localhost:3306/haidao";

        try{
            Class.forName(driver);
            //1.getConnection()方法，连接MySQL数据库！！
            connection=  DriverManager.getConnection(url,user,password);
            if(!connection.isClosed()){
                System.out.println("Succeeded connecting to the Database!");

            }

        }catch (ClassNotFoundException  e){
            //数据库驱动类异常处理
            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();
        } catch(SQLException e) {
            //数据库连接失败异常处理
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public Connection getConnection(){
        return  this.connection;
    }



    // 要执行的SQL语句
    public ResultSet beginSelect(String sql){

        try {
            statement = connection.createStatement();
            rs=  statement.executeQuery(sql);

            int id;
            while(rs.next()){
                //获取stuname这列数据
                id = rs.getInt("id");
                //输出结果
                System.out.println(id + "\t" + id);
            }
            rs.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 获取当前数据库连接情况
     * @return true 表示连接 false 表示断开
     */
    public boolean questConnectionState(){
        try {
            if(connection!=null&&!connection.isClosed()){
                    return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }


    public static void closeConnect(Connection connection){

        try {
            if(connection!=null&&!connection.isClosed()){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void closeConnect(ResultSet rs){

        try {
            if(rs!=null){
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void closeConnect(Statement stmt){

        try {
            if(stmt!=null){
                stmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



}
